<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="收件人id" prop="customerId">
      <el-input v-model="dataForm.customerId" placeholder="收件人id"></el-input>
    </el-form-item>
    <el-form-item label="收件人" prop="customer">
      <el-input v-model="dataForm.customer" placeholder="收件人"></el-input>
    </el-form-item>
    <el-form-item label="平台编号" prop="platformCode">
      <el-input v-model="dataForm.platformCode" placeholder="平台编号"></el-input>
    </el-form-item>
    <el-form-item label="第三方单号" prop="thirdCode">
      <el-input v-model="dataForm.thirdCode" placeholder="第三方单号"></el-input>
    </el-form-item>
    <el-form-item label="快递公司" prop="expressGs">
      <el-input v-model="dataForm.expressGs" placeholder="快递公司"></el-input>
    </el-form-item>
    <el-form-item label="快递单号" prop="expressCode">
      <el-input v-model="dataForm.expressCode" placeholder="快递单号"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="总价格" prop="totalPrice">
      <el-input v-model="dataForm.totalPrice" placeholder="总价格"></el-input>
    </el-form-item>
    <el-form-item label="出库时间" prop="outTime">
      <el-input v-model="dataForm.outTime" placeholder="出库时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="modifyTime">
      <el-input v-model="dataForm.modifyTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          customerId: '',
          customer: '',
          platformCode: '',
          thirdCode: '',
          expressGs: '',
          expressCode: '',
          status: '',
          totalPrice: '',
          outTime: '',
          modifyTime: '',
          createTime: ''
        },
        dataRule: {
          customerId: [
            { required: true, message: '收件人id不能为空', trigger: 'blur' }
          ],
          customer: [
            { required: true, message: '收件人不能为空', trigger: 'blur' }
          ],
          platformCode: [
            { required: true, message: '平台编号不能为空', trigger: 'blur' }
          ],
          thirdCode: [
            { required: true, message: '第三方单号不能为空', trigger: 'blur' }
          ],
          expressGs: [
            { required: true, message: '快递公司不能为空', trigger: 'blur' }
          ],
          expressCode: [
            { required: true, message: '快递单号不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          totalPrice: [
            { required: true, message: '总价格不能为空', trigger: 'blur' }
          ],
          outTime: [
            { required: true, message: '出库时间不能为空', trigger: 'blur' }
          ],
          modifyTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/app/package/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.customerId = data.package.customerId
                this.dataForm.customer = data.package.customer
                this.dataForm.platformCode = data.package.platformCode
                this.dataForm.thirdCode = data.package.thirdCode
                this.dataForm.expressGs = data.package.expressGs
                this.dataForm.expressCode = data.package.expressCode
                this.dataForm.status = data.package.status
                this.dataForm.totalPrice = data.package.totalPrice
                this.dataForm.outTime = data.package.outTime
                this.dataForm.modifyTime = data.package.modifyTime
                this.dataForm.createTime = data.package.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/package/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'customerId': this.dataForm.customerId,
                'customer': this.dataForm.customer,
                'platformCode': this.dataForm.platformCode,
                'thirdCode': this.dataForm.thirdCode,
                'expressGs': this.dataForm.expressGs,
                'expressCode': this.dataForm.expressCode,
                'status': this.dataForm.status,
                'totalPrice': this.dataForm.totalPrice,
                'outTime': this.dataForm.outTime,
                'modifyTime': this.dataForm.modifyTime,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
